package library.dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import library.dao.ConnectedDao;
import library.dao.UserDao;
import library.domain.User;
import library.exception.DataException;

public class UserDaoImpl extends ConnectedDao implements UserDao {

    private static final String READ_REQUEST = "SELECT `identity`, `login`, `password`, `name`, `surname`, `middleName` FROM `users` WHERE `identity` = ?";
    private static final String READ_SOME_REQUEST = "SELECT `identity`, `login`, `password`, `name`, `surname`, `middleName` FROM `users` WHERE `login` LIKE ? AND `password` LIKE ?";

    public UserDaoImpl(Connection connection) {
        super(connection);
    }

    @Override
    public User read(String login, String password) throws DataException {
        User user = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            String sql = READ_SOME_REQUEST;
            statement = connection.prepareStatement(sql);
            statement.setString(1, login);
            statement.setString(2, password);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                user = new User();
                user.setId(resultSet.getInt("identity"));
                user.setLogin(resultSet.getString("login"));
                user.setName(resultSet.getString("name"));
                user.setSurname(resultSet.getString("surname"));
                user.setMiddleName(resultSet.getString("middleName"));
                user.setPassword(resultSet.getString("password"));
            }
            return user;
        } catch (SQLException e) {
            throw new DataException(e);
        } finally {
            close(resultSet, statement);
        }
    }

    @Override
    public User read(Integer identity) throws DataException {
        User user = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            String sql = READ_REQUEST;
            statement = connection.prepareStatement(sql);
            statement.setInt(1, identity);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                user = new User();
                user.setId(resultSet.getInt("identity"));
                user.setLogin(resultSet.getString("login"));
                user.setName(resultSet.getString("name"));
                user.setSurname(resultSet.getString("surname"));
                user.setMiddleName(resultSet.getString("middleName"));
                user.setPassword(resultSet.getString("password"));
            }
            return user;
        } catch (SQLException e) {
            throw new DataException(e);
        } finally {
            close(resultSet, statement);
        }
    }

    @Override
    public Integer create(User entity) throws DataException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<User> readAll() throws DataException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void update(User value) throws DataException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void delete(Integer identity) throws DataException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
